Import the plotly and dplyr packages:
if(!require("plotly")){
install.packages("plotly")
library("plotly")
}
if(!require("dplyr")){
install.packages("dplyr")
library("dplyr")
}
I wrote a web scraper to gets the latitude, longitude, and enrollment of the top 200 universities and saves to a file named top_200_universities.csv.
Import the file:
universities <- read.csv("top_200_universities.csv")
universities <- universities %>% filter(!is.na(Latitude) &
!is.na(Longitude) &
!is.na(Enrollment))
names(universities)[1] = "Rank"
Plot a scatter plot using plotly comparing the enrollment
fig <- plot_ly(type = 'scatter', mode = 'markers')
fig <- fig %>%
add_trace(x = universities$Enrollment,
y = universities$Rank,
text= universities$Name,
hoverinfo = 'text',
marker = list(color='blue'),
showlegend = F)
fig <- fig %>%
layout(title = list(text = "Rank vs. Enrollment for Top 200 U.S. Universities"),
xaxis = list(title = 'Enrollment'),
yaxis = list(title = 'Rank'))
fig